import pygame
from types import SimpleNamespace
from common import (
    white,
    black,
    red,
    green,
    blue,
    font_path,
    pink,
    go_next_level,
    get_score,
    add_score,
)

screen_width, screen_height = 1200, 450
circuit_image_pre = pygame.image.load("./assets/circuit_1-1-pre.png")
circuit_image_post = pygame.image.load("./assets/circuit_1-1-post.png")
circuit_image = circuit_image_pre

title_font = pygame.font.Font(font_path, 30)
button_text_font = pygame.font.Font(font_path, 20)
button_up = SimpleNamespace(
    rect=pygame.Rect(300, 350, 150, 50),
    color=pink,
    text="AJ吸起",
    font_color=black,
)
button_dwon = SimpleNamespace(
    rect=pygame.Rect(500, 350, 150, 50),
    color=green,
    text="AJ打落",
    font_color=black,
)
button_submit = SimpleNamespace(
    rect=pygame.Rect(800, 350, 100, 50),
    color=blue,
    text="提交",
    font_color=black,
)
text_notification = SimpleNamespace(
    rect=pygame.Rect(500, 150, 150, 50),
    text="操作错误，提交进入下一关",
    font_color=red,
)

buttons = [button_up, button_dwon, button_submit]

selected = -1
score = 0


def draw(screen):
    global selected, score, circuit_image
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pass
        elif event.type == pygame.MOUSEBUTTONUP:
            for button in buttons:
                if button.rect.collidepoint(event.pos):
                    if buttons.index(button) in [0, 1] and selected == -1:
                        selected = buttons.index(button)
                        score = 10 if selected == 0 else -10
                        add_score(score)
                    elif buttons.index(button) == 2:
                        selected = 2

    # 绘制背景
    screen.fill(white)
    # 绘制title
    font = title_font
    text = font.render(
        f"当前总得分（{get_score()}）关卡1-1：正确操作使D1亮灯",
        True,
        black,
    )
    text_rect = text.get_rect(center=(screen_width // 2, 50))
    screen.blit(text, text_rect)

    # 绘制电路板
    circuit = pygame.transform.smoothscale(circuit_image, (1200, 220))
    screen.blit(circuit, (0, 100))

    for i, button in enumerate(buttons):
        font = button_text_font
        if selected == i:
            if selected == 2:
                go_next_level()
                selected = -1
            elif selected == 0:
                text = font.render(button.text + "(已选择)", True, button.font_color)
                circuit_image = circuit_image_post
            else:
                text = font.render(button.text + "(已选择)", True, button.font_color)
                # draw notification
                font = button_text_font
                text_no = font.render(
                    text_notification.text, True, text_notification.font_color
                )
                text_no_rect = text.get_rect(center=text_notification.rect.center)
                screen.blit(text_no, text_no_rect)
        else:
            text = font.render(button.text, True, button.font_color)
        text_rect = text.get_rect(center=button.rect.center)
        # 绘制按钮
        pygame.draw.rect(screen, button.color, button.rect)
        screen.blit(text, text_rect)
        # 绘制文字
        screen.blit(text, text_rect)
